//
//  AIDiaryReplayBean.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/3/11.
//
/**
 * 语音来电 - minmax
 */
import Foundation
import SwiftUI
class AIActiveCallMinMaxBean : Codable {
    
    var tokens_to_generate: Int64 = 1034
    var model = "abab6.5s-chat"
    var temperature: Double = 0.9
    var top_p: Double = 0.95
    var messages: [Messages]
    var reply_constraints: ReplyConstraintsc //回复格式
    var bot_setting:[BotSetting] //对每一个机器人的设定
    
    
    init(content : String, time: TimeInterval, botRoleBean: RoleBean) {
        let userRoleBean = RoleBean.get(userManager.user, botRoleBean)
        self.reply_constraints = ReplyConstraintsc(aiRole: botRoleBean)
        let memoryDesc = AppStorageManager.shared.toggleMemoryInject ? configManager.getConfig().getAiDescribe().aiMemoryInjectDesc?.formatAiChatContent(ActiveAIMemoryExt.instance.getAiMemoryStr(botRoleBean: botRoleBean)) ?? "" : ""

        self.bot_setting = [BotSetting(aiRole: botRoleBean, memoryDesc: memoryDesc)]
        self.messages = AIActiveCallMinMaxBean.getMessagesList(content: content, time: time, botRoleBean: botRoleBean, userRoleBean: userRoleBean)
        
    }
    
    /**
     * 模型回复要求.
     */
    class ReplyConstraintsc : Codable{
        var sender_type = "BOT"
        var sender_name = ""  //指定回复的机器人名称。
        var glyph:Glyph? = nil
        
        init(aiRole: RoleBean) {
            self.sender_name = aiRole.name ?? ""
            self.glyph = configManager.getConfig().getAiDescribe().headCall ?? false ? Glyph() : nil
        }
        
        //限制返回
        class Glyph : Codable{
            var type = "raw"
            var raw_glyph = ""
            init() {
                raw_glyph = "\(self.getHeadStr())，{{gen 'content'}}"
            }
            
            // 定义一个函数来获取问候语
            func getHeadStr() -> String {
                let arStrs = ["嗨", "嘿", "哈喽", "喂", "哟", "喂喂", "欸", "()"]
                let randomIndex = Int.random(in: 0..<arStrs.count)
                let randomWord = arStrs[randomIndex]
                
                if randomWord == "()" {
                    // 获取当前时间
                    let calendar = Calendar.current
                    let hour = calendar.component(.hour, from: Date())
                    
                    // 根据时间判断并输出问候语
                    let greeting: String
                    switch hour {
                    case 5...11:
                        greeting = "早上好"
                    case 12...14:
                        greeting = "中午好"
                    case 12...18:
                        greeting = "下午好"
                    default:
                        greeting = "晚上好"
                    }
                    return greeting
                }
                return randomWord
            }
            
        }
        
    }
    
    /**
     * 对每一个机器人的设定。
     * @param content 具体机器人的设定,长度影响接口性能。
     */
    class BotSetting : Codable {
        var bot_name: String
        var content: String
        init(aiRole: RoleBean,memoryDesc: String) {
            bot_name = aiRole.name ?? ""
            content = RoleBean.getBotDesc(aiRole) + memoryDesc
        }
    }
    
    class Messages: Codable {
        var sender_type = "USER"
        var text: String
        var sender_name: String
        
        init(_ content: String,_ botRoleBean: RoleBean) {
            var name = AiChatExt.getName2(botRoleBean)
            if name.isEmpty == true {
                name = "亲爱的"
            }
            self.text = content
            self.sender_name = name
        }
    }
    
    private static func getMessagesList(content : String, time: TimeInterval, botRoleBean: RoleBean,
                                               userRoleBean: RoleBean) ->[Messages] {
        var listMsg : [Messages] = []
        //用户人设
        let userDesc: String = RoleBean.getUserDesc(botRoleBean)
        listMsg.append(Messages(userDesc, botRoleBean))
        //添加时间
        let timeStr = TimeUtils.getDate(time: time, format: "yyyy-MM-dd HH:mm")
        listMsg.append(Messages("现在的时间是：\(timeStr),这条信息仅用于你理解，不用说出来。", botRoleBean))
        //添加内容
        let content2 = content.isEmpty ? "发一些问候语，关心我，了解下我的情况。" : "以下是我今天写的日记:‘\(content)’。请结合记录中的具体内容"
        listMsg.append(Messages(content2, botRoleBean))
        //添加说明
        listMsg.append(Messages((configManager.getConfig().getAiDescribe().callDescribe ?? "").formatAiChat(botRoleBean, time), botRoleBean))
        return listMsg
    }
    
    
}
